package org.journal.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.journal.entity.RecordEntity;
import org.springframework.stereotype.Repository;

@Repository
public class RecordDaoImpl extends GenericDAOImpl<RecordEntity, Integer>
		implements RecordDao {

	public RecordEntity getRecordById(int recordId) {

		return getEntityById(RecordEntity.class, recordId);

	}

	public Integer createRecord(RecordEntity record) {

		return (Integer) save(record);

	}

	public List<RecordEntity> getDailyRecordsByDate(Calendar date) {
		Calendar endDate = (Calendar) date.clone();

		endDate.add(Calendar.SECOND,
				Calendar.SECOND - date.get(Calendar.SECOND));
		endDate.add(Calendar.MINUTE,
				Calendar.MINUTE - date.get(Calendar.MINUTE));
		endDate.add(Calendar.HOUR,
				Calendar.HOUR_OF_DAY - date.get(Calendar.HOUR));

		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"select record from RecordEntity record where record.date between :startDate and :endDate");
		query.setCalendar("startDate", date);
		query.setCalendar("endDate", endDate);
		return search(query);

	}

	public List<RecordEntity> getRecordsByDateInterval(Date start_date,
			Date end_date) {
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"select record from RecordEntity record where record.date between :start_date and :end_date");
		query.setDate("start_date", start_date);
		query.setDate("end_date", end_date);
		return search(query);

	}

}
